<template>
  <el-table :data="tableData" style="width: 100%" row-key="objectId">
    <el-table-column label="类目级别" width="180" prop="parentId">
      <template #default="scope">
        {{ scope.row.parentId == "0-0" ? "顶级类目" : "" }}
      </template>
    </el-table-column>
    <el-table-column label="分类名称" width="180" prop="name">
      <!-- <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #default>
            <div>name: {{ scope.row.name }}</div>
            <div>address: {{ scope.row.address }}</div>
          </template>
          <template #reference>
            <el-tag>{{ scope.row.name }}</el-tag>
          </template>
        </el-popover>
      </template> -->
    </el-table-column>
    <el-table-column label="是否上架" width="180">
      <template #default="scope">
        <el-switch v-model="scope.row.stat"> </el-switch>
      </template>
    </el-table-column>
    <el-table-column label="分类图标" width="180" prop="icon">
    </el-table-column>
    <el-table-column label="Operations">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
          >Edit</el-button
        >
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >Delete</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { categoryGet } from "@/api/job.js";
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    categoryGet(true).then((res) => {
      // console.log(res);
      let { results } = res.data; //未处理的原始数据包
      //提取第一级分类
      let parentArr = results.filter((item) => item.parentId == "0-0");
      //遍历一级分类
      parentArr.forEach((item) => {
        // 从results中找到当前item的子类
        // 依据：item.objectId 等于  results[x].parentId
        let children = results.filter(
          (child) => item.objectId == child.parentId
        );
        item.children = children;
      });
      console.log(parentArr);
      this.tableData = parentArr;
    });
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
  },
};
</script>
